﻿using System.Linq;
using System.Xml.Linq;
using NJabber.Stream.Elements;
using NUnit.Framework;

namespace NJabber.Stream.Elements
{
    [TestFixture]
    public class IqQueryElementTest : XmppStreamElementTestBase
    {
        private string contentNamespace = "test-content-namespace";

        public IqQueryElementTest() : base("query", "", "")
        { }

        #region Write method tests

        [Test]
        public void Write_NoChildElements_WritesCorrectXml()
        {
            IqQueryElement element = new IqQueryElement();
            XElement expectedElement = CreateXElement(Name);
            VerifyWriteOutput(expectedElement, element);
        }

        [Test]
        public void Write_ContainsChildElements_WritesCorrectXml()
        {
            string[] childsNames = {"child1", "child2", "child3"};
            IqQueryElement element = CreateElementWithChilds(contentNamespace, childsNames);
            XElement expectedElement = CreateXElementWithChilds(contentNamespace, childsNames);
            VerifyWriteOutput(expectedElement, element);
        }

        private IqQueryElement CreateElementWithChilds(string contentNamespace, params string[] childsNames)
        {
            IqQueryElement element = new IqQueryElement(contentNamespace);
            foreach (string name in childsNames)
            {
                element.AddContentItem(new XmppStreamElementImpl(name));
            }
            return element;
        }

        private XElement CreateXElementWithChilds(string contentNamespace, params string[] childsNames)
        {
            XElement element = CreateXElement("query", contentNamespace);
            foreach (string name in childsNames)
            {
                element.Add(new XElement(name));
            }
            return element;
        }

        #endregion

        [Test]
        public void AddRosterItem_EmptyContent_AddRosterItemElement()
        {
            IqQueryElement query = new IqQueryElement();

            query.AddRosterItem("amy", "amy@lee.com");

            RosterItemElement item = (RosterItemElement) query.Content.Single();
            Assert.That(item.ItemName, Is.EqualTo("amy"));
            Assert.That(item.Jid, Is.EqualTo((JID)"amy@lee.com"));
        }
    }
}
